/*
 * NetworkThreadManager.java
 *
 * Created on 6 maart 2008, 19:17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package racer.client.network;
import java.io.*;
import java.net.*;
import racer.client.data.PositionMessage;
import racer.client.data.SettingsMessage;
import racer.client.data.ChatMessage;
import racer.client.gui.Game;
import racer.client.network.threads.GoingToServerThread;
import racer.client.network.threads.IncomingFromServerThread;
import racer.client.network.interfaces.INetworkThreadManager;
import java.util.HashMap;

//import threads2.client.threads.Gui;



/**
 *
 * @author Lieven
 */
public class NetworkThreadManager implements INetworkThreadManager{
    public static PositionMessage posmess;
    public static SettingsMessage settingsmess;
    public static ChatMessage chatmess;
    public static Socket clientSocket = null;
    public static PrintStream os = null;
    public static DataInputStream is = null;
    public static BufferedReader inputLine = null;
    public static boolean closed = false;
    public static ThreadGroup networkThreadGroup;
    private static Thread[] threadArray;
    private static String host;
    private static int port_number;
    private Game game;
    public HashMap<String,String> settingsmap;
    
    /**
     * Creates a new instance of NetworkThreadManager
     */
    public NetworkThreadManager(int port_number,String host, Game game) {
    
        //set port and host
        this.host=host;
        this.port_number=port_number;
        this.game = game;
        
        initNetwork();
        initThreads();

        settingsmap = new HashMap<String,String>();


        
    }
    
    
    /**
     * NETWORK INIT
     */
    public void initNetwork(){
        
        //al op voorhand geset
	//int port_number=2222;
        //String host="localhost";
	
	// Initialization section:
	// Try to open a socket on a given host and port
	// Try to open input and output streams
	try {
            clientSocket = new Socket(host, port_number);
            inputLine = new BufferedReader(new InputStreamReader(System.in));
            os = new PrintStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
            
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host "+host);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to the host "+host);
        }
	

        
        
    }

     public void sendPositionToServer(double xvalue,double yvalue,int angle,int active_tile){
        os.println(posmess.getFormedMessage(xvalue,yvalue,angle,active_tile));
    }
    
     public void sendChatMessageToServer(int toID,String text){
        os.println(chatmess.getFormedMessage(toID,text));
    }     
     
     public void sendSettingMessageToServer(int toID,String settingtext){
        os.println(settingsmess.getFormedMessage(toID,settingtext));
    }          
     
     public void sendAllSettingsToServer(){
         //
     }
    /*THREADS MANAGER
     *
     *
     **/
    
    public void initThreads(){
        
        threadArray = new Thread[5];
        networkThreadGroup = new ThreadGroup ("NetworkThreads");
        Thread incomingFromServerThread = new Thread ( networkThreadGroup, new IncomingFromServerThread(this));
        incomingFromServerThread.setName("incomingFromServerThread");
        Thread goingToServerThread = new Thread ( networkThreadGroup, new GoingToServerThread(this));
        goingToServerThread.setName("goingToServerThread");
        
        incomingFromServerThread.start();        
        goingToServerThread.start();
        //set active threads ..copy into array..for later use
        setActiveThreads();
        //print threadarray
        //for (int i = 0; i < threadArray.length; i++){
        //    os.println(threadArray[i]);
        //}
        
        
        
    }
    
    public int getNumberOfActiveThreads(){
        
        return networkThreadGroup.activeCount();
        
    }    

    public void setActiveThreads(){
        networkThreadGroup.enumerate(threadArray);
        
    
   }        
    public void stopNetworkThreads(){
        networkThreadGroup.stop();
        
    }
    
    public void setNetworkThreadsPriority(int np){
        networkThreadGroup.setMaxPriority(np);
    }
    
    // Create an array of threads, to hold all of the active threads

    public Game getGame(){
        return this.game;
    }

    public void setMessageIDs(){
        //message is the form used to send data through the network
        //argument is client ID (voor later)
        int nickid = Integer.parseInt(settingsmap.get("nickid"));
        posmess=new PositionMessage(nickid);        
        chatmess=new ChatMessage(nickid);        
        settingsmess=new SettingsMessage(nickid);
    }
    public void setSetting(String setID,String setValue){
        
        //save settings +send them
        settingsmap.put(setID,setValue);
        
        
        
    }
    

    
}



